package davidlauzon.activerecord.manager;

import java.util.List;

import davidlauzon.activerecord.connection.ConnectionAdapter;
import davidlauzon.activerecord.nodes.SqlStatement;
import davidlauzon.activerecord.nodes.WhereClause;
import davidlauzon.activerecord.visitor.SqlSerializer;


public abstract class StatementManager
{
    protected ConnectionAdapter _connection;
    protected SqlSerializer     _serializer;
    
    
    protected abstract SqlStatement buildStatement();
    
    
    protected ConnectionAdapter getConnection() { return _connection; }
            
    protected void setSqlSerializer(SqlSerializer serializer)  { _serializer = serializer; }
    protected void setConnection(ConnectionAdapter connection) { _connection = connection; }

    
    protected StatementManager() {
        reset();
    }

    public void reset() {
        _connection = null;
        _serializer = null;
    }
    
    public String toSql() {
        return buildStatement().accept( _serializer );
    };
    
    
    protected WhereClause buildWhereClause(String condition, List<Object> unsafeValues) {
        Object[] args = null;
        if ( (unsafeValues != null) && (unsafeValues.size() > 0))
            args = (Object[]) unsafeValues.toArray( new Object[unsafeValues.size()] );
        
        return new WhereClause(condition, args);
    }
}
